package com.es.config;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

//@Configuration
public class DBConfig {
	
	@Autowired
	private Environment env;
	/**
     * 配置DataSource
     * @return
     * @throws SQLException
     */
    public DataSource indexDBDataSource() throws SQLException {
        DruidDataSource druid = new DruidDataSource();
        druid.setName("indexDB");
        druid.setUsername(env.getProperty("jdbc.index.username"));
        druid.setPassword(env.getProperty("jdbc.index.password"));
        druid.setUrl(env.getProperty("jdbc.index.url"));
        druid.setDriverClassName(env.getProperty("jdbc.index.driver"));
       //初始连接数
        druid.setInitialSize(10);
        //最小连接数
        druid.setMinIdle(10);
        //最大连接数
       druid.setMaxActive(100);
       //最大等待时间 10s
        druid.setMaxWait(10000);
        //配置隔多久进行一次检测，检测需要关闭的空闲连接 1分钟
        druid.setTimeBetweenEvictionRunsMillis(60000);
        //一个连接的最小生存时间 5分钟
        druid.setMinEvictableIdleTimeMillis(300000);
        //建议配置为true，不影响性能，并且保证安全性。
        //申请连接的时候检测，如果空闲时间大于timeBetweenEvictionRunsMillis，
        //执行validationQuery检测连接是否有效。
        druid.setTestWhileIdle(true);
        //建议配置为true，防止去到的连接不可用
        druid.setTestOnBorrow(true);
        druid.setTestOnReturn(true);
        //是否缓存preparedStatement，也就是PSCache。
        //PSCache对支持游标的数据库性能提升巨大，比如说oracle。在mysql下建议关闭。
       // druid.setPoolPreparedStatements(false);
        //druid.setMaxPoolPreparedStatementPerConnectionSize(20);
        
        //配置提交方式，默认true 可以不配置
        druid.setDefaultAutoCommit(true);
        //验证连接有效与否的sql，不同的数据配置不同
        druid.setValidationQuery("select 1");
        //属性类型是字符串，通过别名的方式配置扩展插件，常用的插件有： 
        //监控统计用的filter:stat日志用的filter:log4j防御sql注入的filter:wall
        druid.setFilters("stat,wall,log4j");
        return druid;
    }
    
    @Bean(name="indexDBTransactionManager")
    public DataSourceTransactionManager transactionManager() throws SQLException{
    	DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
    	transactionManager.setDataSource(indexDBDataSource());
    	return transactionManager;
    }
    
    @Bean(name="indexDBTemplate")
    public JdbcTemplate cddm2cTemplate() throws SQLException{
    	JdbcTemplate template = new JdbcTemplate(indexDBDataSource());
    	return template;
    }
    
    /**
	 * 注册DruidServlet
	 * @return
	 */
	@Bean
    public ServletRegistrationBean<StatViewServlet> druidServletRegistrationBean() {
        ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<StatViewServlet>();
        servletRegistrationBean.setServlet(new StatViewServlet());
        servletRegistrationBean.addUrlMappings("/druid/*");
        return servletRegistrationBean;
    }
	
	/**
     * 注册DruidFilter拦截
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean<WebStatFilter> duridFilterRegistrationBean() {
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<WebStatFilter>();
        filterRegistrationBean.setFilter(new WebStatFilter());
        Map<String, String> initParams = new HashMap<String, String>();
        //设置忽略请求
        initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.setInitParameters(initParams);
        filterRegistrationBean.addUrlPatterns("/*");
        return filterRegistrationBean;
    }

}
